<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>设计模型 | 欢迎学习django课程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../part1/4.html" />
    
    
    <link rel="prev" href="../part1/2.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="1.2"
        data-chapter-title="设计模型"
        data-filepath="part1/3.md"
        data-basepath=".."
        data-revision="Tue Jan 03 2017 08:43:08 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="part1/1.html">
            
                
                    <a href="../part1/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="part1/2.html">
            
                
                    <a href="../part1/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        搭建开发环境
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="part1/3.html">
            
                
                    <a href="../part1/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        设计模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="part1/4.html">
            
                
                    <a href="../part1/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        管理站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="part1/5.html">
            
                
                    <a href="../part1/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="part1/6.html">
            
                
                    <a href="../part1/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="part1/7.html">
            
                
                    <a href="../part1/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="part2/1.html">
            
                
                    <a href="../part2/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模型
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="part2/2.html">
            
                
                    <a href="../part2/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        定义模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="part2/3.html">
            
                
                    <a href="../part2/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        模型成员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="part2/4.html">
            
                
                    <a href="../part2/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        模型查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="part2/5.html">
            
                
                    <a href="../part2/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="part2/6.html">
            
                
                    <a href="../part2/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="part3/1.html">
            
                
                    <a href="../part3/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="part3/2.html">
            
                
                    <a href="../part3/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        URLconf
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="part3/3.html">
            
                
                    <a href="../part3/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="part3/4.html">
            
                
                    <a href="../part3/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Request对象
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="part3/4_1.html">
            
                
                    <a href="../part3/4_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        QueryDict对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="part3/4_2.html">
            
                
                    <a href="../part3/4_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GET属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="part3/4_3.html">
            
                
                    <a href="../part3/4_3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        POST属性
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="part3/5.html">
            
                
                    <a href="../part3/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Response对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="part3/6.html">
            
                
                    <a href="../part3/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        状态保持
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="part3/7.html">
            
                
                    <a href="../part3/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="part4/1.html">
            
                
                    <a href="../part4/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="part4/2.html">
            
                
                    <a href="../part4/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        定义模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="part4/3.html">
            
                
                    <a href="../part4/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        模板继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="part4/4.html">
            
                
                    <a href="../part4/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        HTML转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="part4/5.html">
            
                
                    <a href="../part4/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        CSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="part4/6.html">
            
                
                    <a href="../part4/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="part4/7.html">
            
                
                    <a href="../part4/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="part5/1.html">
            
                
                    <a href="../part5/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="part5/2.html">
            
                
                    <a href="../part5/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        静态文件处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="part5/3.html">
            
                
                    <a href="../part5/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="part5/4.html">
            
                
                    <a href="../part5/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="part5/5.html">
            
                
                    <a href="../part5/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="part5/6.html">
            
                
                    <a href="../part5/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="part5/7.html">
            
                
                    <a href="../part5/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        ajax
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="part5/8.html">
            
                
                    <a href="../part5/8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="part6/1.html">
            
                
                    <a href="../part6/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第三方
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="part6/2.html">
            
                
                    <a href="../part6/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        富文本编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="part6/3.html">
            
                
                    <a href="../part6/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="part6/4.html">
            
                
                    <a href="../part6/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        全文检索
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="part6/5.html">
            
                
                    <a href="../part6/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        celery
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="part6/6.html">
            
                
                    <a href="../part6/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        布署
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="part6/7.html">
            
                
                    <a href="../part6/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="part7/1.html">
            
                
                    <a href="../part7/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        Git与项目
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="part7/2.html">
            
                
                    <a href="../part7/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        Git简介
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="part7/2_2.html">
            
                
                    <a href="../part7/2_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.1.</b>
                        
                        远程仓库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="part7/2_1.html">
            
                
                    <a href="../part7/2_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.2.</b>
                        
                        本地仓库
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="part7/3.html">
            
                
                    <a href="../part7/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        项目设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="part7/4.html">
            
                
                    <a href="../part7/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        模型类设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="part7/5.html">
            
                
                    <a href="../part7/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        使用页面
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="part7/6.html">
            
                
                    <a href="../part7/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >欢迎学习django课程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x8BBE;&#x8BA1;&#x4ECB;&#x7ECD;">&#x8BBE;&#x8BA1;&#x4ECB;&#x7ECD;</h1>
<ul>
<li>&#x672C;&#x793A;&#x4F8B;&#x5B8C;&#x6210;&#x201C;&#x56FE;&#x4E66;-&#x82F1;&#x96C4;&#x201D;&#x4FE1;&#x606F;&#x7684;&#x7EF4;&#x62A4;&#xFF0C;&#x9700;&#x8981;&#x5B58;&#x50A8;&#x4E24;&#x79CD;&#x6570;&#x636E;&#xFF1A;&#x56FE;&#x4E66;&#x3001;&#x82F1;&#x96C4;</li>
<li>&#x56FE;&#x4E66;&#x8868;&#x7ED3;&#x6784;&#x8BBE;&#x8BA1;&#xFF1A;<ul>
<li>&#x8868;&#x540D;&#xFF1A;BookInfo</li>
<li>&#x56FE;&#x4E66;&#x540D;&#x79F0;&#xFF1A;btitle</li>
<li>&#x56FE;&#x4E66;&#x53D1;&#x5E03;&#x65F6;&#x95F4;&#xFF1A;bpub_date</li>
</ul>
</li>
<li>&#x82F1;&#x96C4;&#x8868;&#x7ED3;&#x6784;&#x8BBE;&#x8BA1;&#xFF1A;<ul>
<li>&#x8868;&#x540D;&#xFF1A;HeroInfo</li>
<li>&#x82F1;&#x96C4;&#x59D3;&#x540D;&#xFF1A;hname</li>
<li>&#x82F1;&#x96C4;&#x6027;&#x522B;&#xFF1A;hgender</li>
<li>&#x82F1;&#x96C4;&#x7B80;&#x4ECB;&#xFF1A;hcontent</li>
<li>&#x6240;&#x5C5E;&#x56FE;&#x4E66;&#xFF1A;hbook</li>
</ul>
</li>
<li>&#x56FE;&#x4E66;-&#x82F1;&#x96C4;&#x7684;&#x5173;&#x7CFB;&#x4E3A;&#x4E00;&#x5BF9;&#x591A;</li>
</ul>
<h1 id="&#x6570;&#x636E;&#x5E93;&#x914D;&#x7F6E;">&#x6570;&#x636E;&#x5E93;&#x914D;&#x7F6E;</h1>
<ul>
<li>&#x5728;settings.py&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x901A;&#x8FC7;DATABASES&#x9879;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x5E93;&#x8BBE;&#x7F6E;</li>
<li>django&#x652F;&#x6301;&#x7684;&#x6570;&#x636E;&#x5E93;&#x5305;&#x62EC;&#xFF1A;sqlite&#x3001;mysql&#x7B49;&#x4E3B;&#x6D41;&#x6570;&#x636E;&#x5E93;</li>
<li><strong>Django&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;SQLite&#x6570;&#x636E;&#x5E93;</strong></li>
</ul>
<h1 id="&#x521B;&#x5EFA;&#x5E94;&#x7528;">&#x521B;&#x5EFA;&#x5E94;&#x7528;</h1>
<ul>
<li>&#x5728;&#x4E00;&#x4E2A;&#x9879;&#x76EE;&#x4E2D;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4E00;&#x5230;&#x591A;&#x4E2A;&#x5E94;&#x7528;&#xFF0C;&#x6BCF;&#x4E2A;&#x5E94;&#x7528;&#x8FDB;&#x884C;&#x4E00;&#x79CD;&#x4E1A;&#x52A1;&#x5904;&#x7406;</li>
<li>&#x521B;&#x5EFA;&#x5E94;&#x7528;&#x7684;&#x547D;&#x4EE4;&#xFF1A;</li>
</ul>
<pre><code>python manage.py startapp booktest
</code></pre><ul>
<li>&#x5E94;&#x7528;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#x56FE;</li>
</ul>
<p><img src="images/app.png" alt="&#x5E94;&#x7528;&#x9ED8;&#x8BA4;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"></p>
<h4 id="&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7C7B;">&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7C7B;</h4>
<ul>
<li>&#x6709;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x8868;&#xFF0C;&#x5C31;&#x6709;&#x4E00;&#x4E2A;&#x6A21;&#x578B;&#x7C7B;&#x4E0E;&#x4E4B;&#x5BF9;&#x5E94;</li>
<li>&#x6253;&#x5F00;models.py&#x6587;&#x4EF6;&#xFF0C;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7C7B;</li>
<li>&#x5F15;&#x5165;&#x5305;from django.db import models</li>
<li>&#x6A21;&#x578B;&#x7C7B;&#x7EE7;&#x627F;&#x81EA;models.Model&#x7C7B;</li>
<li><strong>&#x8BF4;&#x660E;&#xFF1A;&#x4E0D;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x5217;&#xFF0C;&#x5728;&#x751F;&#x6210;&#x65F6;&#x4F1A;&#x81EA;&#x52A8;&#x6DFB;&#x52A0;&#xFF0C;&#x5E76;&#x4E14;&#x503C;&#x4E3A;&#x81EA;&#x52A8;&#x589E;&#x957F;</strong></li>
<li>&#x5F53;&#x8F93;&#x51FA;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x7684;str&#x65B9;&#x6CD5;</li>
</ul>
<pre><code>from django.db import models

class BookInfo(models.Model):
    btitle = models.CharField(max_length=20)
    bpub_date = models.DateTimeField()
    def _ _str_ _(self):
        return &quot;%d&quot; % self.pk

class HeroInfo(models.Model):
    hname = models.CharField(max_length=20)
    hgender = models.BooleanField()
    hcontent = models.CharField(max_length=100)
    hBook = models.ForeignKey(&apos;BookInfo&apos;)
    def _ _str_ _(self):
        return &quot;%d&quot; % self.pk
</code></pre><h4 id="&#x751F;&#x6210;&#x6570;&#x636E;&#x8868;">&#x751F;&#x6210;&#x6570;&#x636E;&#x8868;</h4>
<ul>
<li>&#x6FC0;&#x6D3B;&#x6A21;&#x578B;&#xFF1A;&#x7F16;&#x8F91;settings.py&#x6587;&#x4EF6;&#xFF0C;&#x5C06;booktest&#x5E94;&#x7528;&#x52A0;&#x5165;&#x5230;installed_apps&#x4E2D;</li>
</ul>
<p><img src="images/applist.png" alt="&#x5E94;&#x7528;&#x5217;&#x8868;"></p>
<ul>
<li>&#x751F;&#x6210;&#x8FC1;&#x79FB;&#x6587;&#x4EF6;&#xFF1A;&#x6839;&#x636E;&#x6A21;&#x578B;&#x7C7B;&#x751F;&#x6210;sql&#x8BED;&#x53E5;</li>
</ul>
<pre><code>python manage.py makemigrations
</code></pre><ul>
<li>&#x8FC1;&#x79FB;&#x6587;&#x4EF6;&#x88AB;&#x751F;&#x6210;&#x5230;&#x5E94;&#x7528;&#x7684;migrations&#x76EE;&#x5F55;</li>
</ul>
<p><img src="images/migrate.png" alt="&#x8FC1;&#x79FB;&#x6587;&#x4EF6;"></p>
<ul>
<li>&#x6267;&#x884C;&#x8FC1;&#x79FB;&#xFF1A;&#x6267;&#x884C;sql&#x8BED;&#x53E5;&#x751F;&#x6210;&#x6570;&#x636E;&#x8868;</li>
</ul>
<pre><code>python manage.py migrate
</code></pre><h4 id="&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x64CD;&#x4F5C;">&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x64CD;&#x4F5C;</h4>
<ul>
<li>&#x8FDB;&#x5165;python shell&#xFF0C;&#x8FDB;&#x884C;&#x7B80;&#x5355;&#x7684;&#x6A21;&#x578B;API&#x7EC3;&#x4E60;</li>
</ul>
<pre><code>python manage.py shell
</code></pre><ul>
<li>&#x8FDB;&#x5165;shell&#x540E;&#x63D0;&#x793A;&#x5982;&#x4E0B;&#xFF1A;</li>
</ul>
<p><img src="images/shell.png" alt="python shell"></p>
<ul>
<li>&#x5F15;&#x5165;&#x9700;&#x8981;&#x7684;&#x5305;&#xFF1A;</li>
</ul>
<pre><code>from booktest.models import BookInfo,HeroInfo
from django.utils import timezone
from datetime import *
</code></pre><ul>
<li>&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x56FE;&#x4E66;&#x4FE1;&#x606F;&#xFF1A;</li>
</ul>
<pre><code>BookInfo.objects.all()
</code></pre><ul>
<li>&#x65B0;&#x5EFA;&#x56FE;&#x4E66;&#x4FE1;&#x606F;&#xFF1A;</li>
</ul>
<pre><code>b = BookInfo()
b.btitle=&quot;&#x5C04;&#x96D5;&#x82F1;&#x96C4;&#x4F20;&quot;
b.bpub_date=datetime(year=1990,month=1,day=10)
b.save()
</code></pre><ul>
<li>&#x67E5;&#x627E;&#x56FE;&#x4E66;&#x4FE1;&#x606F;&#xFF1A;</li>
</ul>
<pre><code>b=BookInfo.objects.get(pk=1)
</code></pre><ul>
<li>&#x8F93;&#x51FA;&#x56FE;&#x4E66;&#x4FE1;&#x606F;&#xFF1A;</li>
</ul>
<pre><code>b
b.id
b.btitle
</code></pre><ul>
<li>&#x4FEE;&#x6539;&#x56FE;&#x4E66;&#x4FE1;&#x606F;&#xFF1A;</li>
</ul>
<pre><code>b.btitle=u&quot;&#x5929;&#x9F99;&#x516B;&#x90E8;&quot;
b.save()
</code></pre><ul>
<li>&#x5220;&#x9664;&#x56FE;&#x4E66;&#x4FE1;&#x606F;&#xFF1A;</li>
</ul>
<pre><code>b.delete()
</code></pre><h4 id="&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;&#x64CD;&#x4F5C;">&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;&#x64CD;&#x4F5C;</h4>
<ul>
<li>&#x5BF9;&#x4E8E;HeroInfo&#x53EF;&#x4EE5;&#x6309;&#x7167;&#x4E0A;&#x9762;&#x7684;&#x64CD;&#x4F5C;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;</li>
<li>&#x6DFB;&#x52A0;&#xFF0C;&#x6CE8;&#x610F;&#x6DFB;&#x52A0;&#x5173;&#x8054;&#x5BF9;&#x8C61;</li>
</ul>
<pre><code>h=HeroInfo()
h.htitle=u&apos;&#x90ED;&#x9756;&apos;
h.hgender=True
h.hcontent=u&apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;
h.hBook=b
h.save()
</code></pre><ul>
<li>&#x83B7;&#x5F97;&#x5173;&#x8054;&#x96C6;&#x5408;&#xFF1A;&#x8FD4;&#x56DE;&#x5F53;&#x524D;book&#x5BF9;&#x8C61;&#x7684;&#x6240;&#x6709;hero</li>
</ul>
<pre><code>b.heroinfo_set.all()
</code></pre><ul>
<li>&#x6709;&#x4E00;&#x4E2A;HeroInfo&#x5B58;&#x5728;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x6709;&#x4E00;&#x4E2A;BookInfo&#x5BF9;&#x8C61;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x521B;&#x5EFA;&#x5173;&#x8054;&#x7684;&#x6570;&#x636E;&#xFF1A;</li>
</ul>
<pre><code>h=b.heroinfo_set.create(htitle=u&apos;&#x9EC4;&#x84C9;&apos;,hgender=False,hcontent=u&apos;&#x6253;&#x72D7;&#x68CD;&#x6CD5;&apos;)
h
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../part1/2.html" class="navigation navigation-prev " aria-label="Previous page: 搭建开发环境"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../part1/4.html" class="navigation navigation-next " aria-label="Next page: 管理站点"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
